package com.yango.oss.controller;

import com.yango.commonutil.result.Result;
import com.yango.oss.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author Li.Ding.yin
 * @create 2021年07月23日21:01-星期五
 * @role oss-前端控制器
 */
@Api(description = "oss-前端控制器")
@RestController
@RequestMapping("/oss/file")
public class FileController {

    @Autowired
    private FileService fileService;

    //图片上传，返回上传之后图片URL路径
    @ApiOperation(value = "图片上传，返回上传之后图片URL路径")
    @PostMapping("/upload")
    public Result upload(@ApiParam(name = "file", value = "请选择文件", required = true)
                         @RequestParam("file") MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        String url = fileService.upload(file);
        if (!StringUtils.isEmpty(url)) {
            return Result.ok().data("url", url).message("文件上传成功!");
        } else {
            return Result.error().message("文件上传失败!");
        }

    }
}
